package cloudspace.stats;

import java.util.Properties;

import org.apache.log4j.Logger;


public abstract class AbstractStat implements StatisticType
{
    protected Logger logger = Logger.getLogger( StatIncr.class );

    protected Long value;

    protected String key;

    protected int bronze;

    protected int silver;

    protected int gold;

    protected String units;

    protected String userName;

    protected Properties prop;


    public void setup(
        String key,
        int bronze,
        int silver,
        int gold,
        String units )
    {
        value = new Long( 0 );
        this.key = key;
        this.bronze = bronze;
        this.silver = silver;
        this.gold = gold;
        this.units = units;
    }


    public void setup(
        String key,
        String bronze,
        String silver,
        String gold,
        String units )
    {
        setup( key,
            Integer.parseInt( bronze ),
            Integer.parseInt( silver ),
            Integer.parseInt( gold ),
            units );
    }


    public void setup(
        String key,
        int bronze,
        int silver,
        int gold,
        String units,
        String userName )
    {
        this.userName = userName;
        setup( key, bronze, silver, gold, units );

    }


    public String toString()
    {
        return key + ": " + value;
    }


    public Level getLevel()
    {
        if ( value < bronze )
        {
            return Level.NONE;
        }
        else if ( value < silver )
        {
            return Level.BRONZE;
        }
        else if ( value < gold )
        {
            return Level.SILVER;
        }
        return Level.GOLD;
    }


    public String getKey()
    {
        return key;
    }


    public String getUnits()
    {
        return units;
    }


    public StatisticType copy( String userName, Properties prop )
    {
        AbstractStat newAchieve = (AbstractStat)this.instanceOf();
        newAchieve.setup( this.key,
            this.bronze,
            this.silver,
            this.gold,
            this.units,
            userName );
        String persValue = (String)prop.get( this.key );
        if ( persValue != null )
            newAchieve.value = Long.valueOf( persValue );
        newAchieve.prop = prop;
        return newAchieve;
    }


    public void persist()
    {
        if ( prop == null )
        {
            logger.error( "Persistance not setup properly" );
            return;
        }
        prop.setProperty( key, value.toString() );
    }

}
